package com.hds.modules.app.controller;

import com.hds.common.utils.R;
import com.hds.modules.app.annotation.Login;
import com.hds.modules.app.annotation.LoginUser;
import com.hds.modules.app.entity.ActivationCodeEntity;
import com.hds.modules.app.entity.UserEntity;
import com.hds.modules.app.entity.dto.TempUserInfo;
import com.hds.modules.app.entity.dto.UserEntityDto;
import com.hds.modules.app.form.LoginForm;
import com.hds.modules.app.service.ActivationCodeService;
import com.hds.modules.app.service.UserService;
import com.hds.modules.app.utils.DateUtils;
import com.hds.modules.app.utils.JwtUtils;
import com.hds.modules.sys.controller.AbstractController;
import com.hds.modules.sys.entity.base.SysCompanyEntity;
import com.hds.modules.sys.entity.base.SysMemberShipEntity;
import com.hds.modules.sys.service.base.SysCompanyService;
import com.hds.modules.sys.service.base.SysMemberShipService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author guonan
 * @Email guonan@yewcn.com
 * @Description
 * @Date 2018/6/19 17:20
 */
@RestController
@RequestMapping("/app/user")
public class AppUserController extends AbstractController{

    @Autowired
    UserService userService;

    @Autowired
    private ActivationCodeService activationCodeService;

    @Autowired
    private SysMemberShipService sysMemberShipService;

    @Autowired
    private SysCompanyService sysCompanyService;

    @Autowired
    private JwtUtils jwtUtils;

    @Login
    @RequestMapping("/userInfo")
    public R queryUserInfo(@LoginUser UserEntity user){

        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getUserId());
        map.put("status", 1);
        List<ActivationCodeEntity> list = activationCodeService.selectByMap(map);

        UserEntityDto userDto = new UserEntityDto();
        ActivationCodeEntity code = null;
        Date expireTime = null; //会员过期时间
        String companyName = null; //该激活码所属公司

        if(list!=null && list.size()>0){
            code = list.get(0);
        }
        Integer levelId = code.getLevelId();

        SysMemberShipEntity ship = sysMemberShipService.selectById(levelId);
        if(ship!=null){
            userDto.setLevel(ship);
        }

        if(code!=null){
            expireTime = code.getExpireTime();
            Integer companyId = code.getCompanyId();
            SysCompanyEntity company = sysCompanyService.selectById(companyId);
            if(company!=null) companyName = company.getName();
        }

        Long days = DateUtils.timespaceOfDays(new Date(), expireTime);


        try {
            BeanUtils.copyProperties(userDto, user);
        }catch (Exception e) {
            e.printStackTrace();
        }

        userDto.setCompanyName(companyName);
        userDto.setDays(Integer.valueOf(String.valueOf(days)));

        return R.ok().put("user",userDto);
    }



    //@Login
    @RequestMapping("/info")
    public TempUserInfo queryUserInfoTemp(@RequestParam("mobile") String mobile){

        //登录
        LoginForm form = new LoginForm();
        form.setMobile(mobile);
        //用户登录
        long userId = userService.login(form);
        UserEntity user = userService.selectById(userId);






        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getUserId());
        map.put("status", 1);
        List<ActivationCodeEntity> list = activationCodeService.selectByMap(map);

        UserEntityDto userDto = new UserEntityDto();
        ActivationCodeEntity code = null;
        Date expireTime = null; //会员过期时间
        String companyName = null; //该激活码所属公司
        Integer levelId = null;
        Long days=0L;

        if(list!=null && list.size()>0){
            code = list.get(0);
            levelId = code.getLevelId();

            SysMemberShipEntity ship = sysMemberShipService.selectById(levelId);
            if(ship!=null){
                userDto.setLevel(ship);
            }

            expireTime = code.getExpireTime();
            Integer companyId = code.getCompanyId();
            SysCompanyEntity company = sysCompanyService.selectById(companyId);
            if(company!=null) companyName = company.getName();

            days = DateUtils.timespaceOfDays(new Date(), expireTime);
        }else{
            SysMemberShipEntity ship = sysMemberShipService.selectById(1);
            userDto.setLevel(ship);
        }





        try {
            BeanUtils.copyProperties(userDto, user);
        }catch (Exception e) {
            e.printStackTrace();
        }

        userDto.setCompanyName(companyName);
        userDto.setDays(Integer.valueOf(String.valueOf(days)));


//        String avatar = userDto.getAvatar();
//        String nickName = userDto.getNickName();
//        int couponNum = userDto.getCouponCount();
//        int existNoPayOrder = userDto.getOrderCount();
//        int isAddress = userDto.getAddressCount();
//        int isCertified = userDto.getIsCertified();
//        long uid = userDto.getUserId();
//        String phone = userDto.getMobile();
        int vipDay = userDto.getDays();

        SysMemberShipEntity level = userDto.getLevel();
        String content = level.getContent();
        int id = level.getId();
        int lev = level.getLevel();
        String logo = level.getLogo();
        String levName = level.getName();

        TempUserInfo userInfo = new TempUserInfo();
        userInfo.setCompanyName(companyName);
//        userInfo.setContactNickName(nickName);
//        userInfo.setCouponNum(couponNum);
//        if(existNoPayOrder == 0)userInfo.setExistNoPayOrder(false);
//        userInfo.setIsAddress(isAddress);
//        if(isCertified == 0){
//            userInfo.setStatus("unrecognized");
//        }
//        userInfo.setUsername(mobile);
        userInfo.setVipContent(content);
        userInfo.setVipDay(vipDay);
        userInfo.setVipId(id+"");
        userInfo.setVipLevel(lev+"");
        userInfo.setVipLogo(logo);
        userInfo.setVipName(levName);
//        userInfo.setWxUserHeadimgurl(avatar);



        return userInfo;
    }

}
